package com.itany.shop.service.proxy;

import com.itany.shop.entity.User;
import com.itany.shop.exception.*;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.UserService;
import com.itany.shop.tran.TransactionManager;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/4/14 14:21
 * Version:1.0-SNAPSHOT
 * Description:
 */
public class UserServiceProxy implements UserService {

    private TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
    private UserService userService = (UserService) ObjectFactory.getObject("userServiceTarget");

    @Override
    public void register(String username, String password, String phone, String address) throws RequestParameterErrorException, UserExistException, ServiceException {
        try {
            tran.begin();
            userService.register(username, password, phone, address);
            tran.commit();
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (RequestParameterErrorException e) {
            tran.rollback();
            throw e;
        } catch (UserExistException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public User login(String username, String password, String code, String image) throws RequestParameterErrorException, CodeErrorException, UserNotExistException, ServiceException {
        try {
            tran.begin();
            User user = userService.login(username,password,code,image);
            tran.commit();
            return user;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        } catch (RequestParameterErrorException e) {
            tran.rollback();
            throw e;
        } catch (CodeErrorException e) {
            tran.rollback();
            throw e;
        } catch (UserNotExistException e) {
            tran.rollback();
            throw e;
        }
    }
}
